#pragma once

#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "LogMessage.hpp"
#include "Err.hpp"


const int backlog = 128;

class Sock
{
public:
    static int Socket()//创建套接字
    {
        int sockfd = socket(AF_INET, SOCK_STREAM, 0);

        return sockfd;
    }
    static bool Bind(uint16_t port, int sockfd)//绑定
    {
        struct sockaddr_in local;
        bzero(&local, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(port);
        local.sin_addr.s_addr = INADDR_ANY;

        if (bind(sockfd, (struct sockaddr *)&local, sizeof(local)) != 0) return false;
        return true;
    }
    static bool Listen(int listensockfd)//监听
    {

        if (listen(listensockfd, backlog) != 0)
        {
            return false;

        }
        return true;
    }
    static int Accept(int listensockfd, std::string *clientip, uint16_t *clientport)//获取
    {
        struct sockaddr_in client;
        socklen_t clientlen = sizeof(client);
        int sock = accept(listensockfd, (struct sockaddr *)&client, &clientlen);
        *clientip = inet_ntoa(client.sin_addr);
        *clientport = client.sin_port;

        return sock;
    }
public:
};